
#include "demo/mercator.h"

#include <math.h>

namespace mercator{

bool GeographyToCoordinate(double b, double l, double &x, double &y) {
  double f, e, e_, NB0, K, dtemp;
  double E = exp(1);

  if(l < -kPI || l > kPI || b < -kPI/2 || b > kPI/2) return false;

  f = (kBigSemiaxis - kSmallSemiaxis) / kBigSemiaxis;

  dtemp = 1 - (kSmallSemiaxis/kBigSemiaxis) * (kSmallSemiaxis/kBigSemiaxis);
  if(dtemp < 0) return false;

  e = sqrt(dtemp);
  dtemp = (kBigSemiaxis/kSmallSemiaxis) * (kBigSemiaxis/kSmallSemiaxis) - 1;
  if(dtemp < 0) return false;

  e_ = sqrt(dtemp);
  NB0 = ((kSmallSemiaxis * kSmallSemiaxis) / kBigSemiaxis) / sqrt(1 + e_*e_*cos(kStandardLatitude)*cos(kStandardLatitude));
  K = NB0 * cos(kStandardLatitude);

  y = K * (l - kStandardLongitude);
  x = K * log(tan(kPI/4 + b/2) * pow((1-e*sin(b)) / (1+e*sin(b)),e/2));

  return true;
}

bool CoordinateToGeography(double x, double y, double &b, double &l) {
  return false;
}

}  // namespace mercator
